package ru.smc.hogwarts.servlets.messages;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ru.smc.hogwarts.classes.EMF;
import ru.smc.hogwarts.model.Message;

public class MessagesList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private HttpSession session;
	private String error = "";	
	
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EntityManagerFactory emf = EMF.getInstance().getEMF();
   	    EntityManager em = emf.createEntityManager();
		session = request.getSession(true);
		List<Message> messages = null;
		String action = request.getParameter("page");
		String action2 = request.getParameter("do");
		session.setAttribute("servletName", getServletName());
		error = "";
		Query query = null;
		int uid = (Integer)session.getAttribute("userID");
		query = em.createQuery("select p from Message p where (p.sender.id=?1 and p.deleteteds=false) or (p.recipient.id =?1 and p.deletetedr=false)");
    	query.setParameter(1,uid);
    	try{
			if (action2.equals("in")) {
				query = em.createQuery("select p from Message p where p.recipient.id =?1 and p.deletetedr=false");
		    	query.setParameter(1,(Integer)session.getAttribute("userID"));
		    	request.setAttribute("action2", "do="+action2+"&");
			}
			if (action2.equals("out")) {
				query = em.createQuery("select p from Message p where p.sender.id=?1 and p.deleteteds=false");
		    	query.setParameter(1,uid);
		    	request.setAttribute("action2", "do="+action2+"&");
			}
		}
    	catch (NullPointerException e){}
    	try {
	    	int size = query.getResultList().size();	
	    	request.setAttribute("size", size);
	    	if (action == null) {
				session.setAttribute("pageBegin", 0);
	    		session.setAttribute("pageEnd", 8);
			}
			else {
	    		int begin = 0;
	    		int end = 8;
	    		
	    		try {
	    			begin = (Integer)session.getAttribute("pageBegin");
	    			end = (Integer)session.getAttribute("pageEnd");
	    		} catch (NullPointerException e) {}
	    		
	    		if (action.equals("next") && end+1 < size) {
	    			begin += 9;
	    			end += 9;
	    		} else if (action.equals("previous") && (begin > 0)) {
	    			begin -= 9;
	    			end -= 9;
	    		}
	    		
	    		session.setAttribute("pageBegin", begin);
	    		session.setAttribute("pageEnd", end);
	    	}
	    	query.setFirstResult((Integer)session.getAttribute("pageBegin"));
	        query.setMaxResults(9);	
	    	messages = query.getResultList();
	    	em.close();
	    	request.setAttribute("error", error);
			request.setAttribute("messages", messages);
			request.getRequestDispatcher("/pages/messages/messagesList.jsp").forward(request, response);
			
	    	if (size==0){
	    		error="Сообщений нет";
	    		request.setAttribute("error", error);
	    		//request.getRequestDispatcher("/pages/messages/messagesList.jsp").forward(request, response);
	    	}
    	}
    	catch (NoResultException e) {
    		error="Сообщений нет";
    		request.setAttribute("error", error);
    		//request.getRequestDispatcher("/pages/messages/messagesList.jsp").forward(request, response);
    	}
	}
}

